import 'package:flutter/material.dart';
import 'package:flutter_fun_app/page/splashPage.dart';
import 'package:flutter_fun_app/page/homePage.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fluintl/fluintl.dart';
import 'package:flutter_fun_app/utils/strings.dart';
import 'package:flutter_fun_app/utils/eventBus.dart';
import 'package:flutter_fun_app/utils/setting.dart';

void main() async {
  await SpUtil.getInstance();
  runApp(MyApp());
  //沉浸狀態欄
//  SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(statusBarColor:Colors.transparent);
//  SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
}

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  Locale _locale;

  var bus = new EventBus();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setLocalizedSimpleValues(localizedSimpleValues);
    bus.on('changeLang', (local) {
      setState(() {
        _locale = local;
      });
      print('changeLang:' + local.toString());
      bus.emit('changeLangChild', local);
    });

    String spLang = Setting.getLang();
    Locale initLocale;
    if (spLang == Setting.EN) {
      initLocale = new Locale('en', '');
    } else {
      Setting.setLang(Setting.ZH);
      initLocale = new Locale('zh', 'CN');
    }
    setState(() {
      _locale = initLocale;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
          primaryColorBrightness: Brightness.dark,
          accentColor: Colors.deepOrange,
          primaryColor: Colors.deepOrange,
          primarySwatch: Colors.deepOrange,
          platform: TargetPlatform.iOS),
      home: HomePage(),
      locale: _locale,
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        CustomLocalizations.delegate
      ],
      supportedLocales: CustomLocalizations.supportedLocales,
      routes: <String, WidgetBuilder>{
        "homePage": (_) => new HomePage(),
      },
    );
  }
}
